package fr.demos.cinepantin.controleur;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.demos.cinepantin.Article;
import fr.demos.cinepantin.LignePanier;
import fr.demos.cinepantin.Panier;
import fr.demos.cinepantin.dao.DaoClient;
import fr.demos.cinepantin.dao.DaoPanier;
import fr.demos.cinepantin.dao.interfac.ClientDao;
import fr.demos.cinepantin.dao.interfac.PanierDao;
import fr.demos.cinepantin.exception.DaoExceptions;
import fr.demos.cinepantin.exception.StockException;

/**
 * @author CQP-DNT
 * Servlet implementation class Facture
 */
@WebServlet("/AfficherFacture")
public class AfficherFacture extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@Inject
	@DaoPanier
	private PanierDao panierdao;
	
	@Inject
	@DaoClient 
	private ClientDao clientdao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AfficherFacture() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Integer idPanier = null;
		
		try{
			idPanier=Integer.parseInt(request.getParameter("idPanier"));
			
		}catch(NumberFormatException nfe){
			
			request.setAttribute("page","afficherArticle.jsp");
			request.getRequestDispatcher("page.jsp").forward(request, response);
			
		}

		// On récupère le panier de la commande en  session
		Panier panier = (Panier) session.getAttribute("panierSession");
		
		try{
			Panier panierCommande = panierdao.getPanierById(idPanier);

		
		// On récupère la liste des articles du panier
		HashMap<Article, List<LignePanier>> articlesCommander;
		articlesCommander = (HashMap <Article, List<LignePanier>>) session.getAttribute("articlesCommander");
		
		} catch (DaoExceptions e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", "Erreur affichage facture !!");
		}


		request.setAttribute("page", "/afficherFacture.jsp");
		RequestDispatcher rd = request.getRequestDispatcher("/page.jsp");
		rd.forward(request, response);
	}	
	


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
	}
	}


